""" 本文件用于编写对应数据库表每一行的类 """

import re
from datetime import datetime
from sqlhlper import SQLHLPER
from itertools import chain

""" 对应mrinfo表的行 """
class MR(object):
    def __init__(self,*,id=0,mr_num='',hospital_num='',pat_name='',
    id_num='',pd='',in_date='',dis_date='',dis_group='',doctor='',
    dis_way='',addr='',filing_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),is_on=1):
        self.__id=id
        self.__mr_num=mr_num
        self.__hospital_num=hospital_num
        self.__pat_name=pat_name
        self.__id_num=id_num
        self.__pd=pd
        self.__in_date=in_date
        self.__dis_date=dis_date
        self.__doctor=doctor
        self.__dis_way=dis_way
        self.__addr=addr
        self.__filing_time=filing_time
        self.__is_on=is_on

        if dis_group=='':
            sqlstr="select group_name from groupname"
            rs=SQLHLPER.selectall(sqlstr)
            self.__dis_group=list(chain.from_iterable(rs))
        else:
            self.__dis_group=dis_group

        self.regex_time=r'^[12][09][0-9]{2}-[01][0-9]-[0-3][0-9](\s{1,4}[0-2][0-9]:[0-5][0-9]:[0-5][0-9])?$'
        self.regex_idnum=r'^([0-9]{17}[0-9a-zA-Z])|([0-9]{15})|(.{0})$'
    
    #验证日期有效性
    def validdate(self,year,month,day,msg='无效日期'):
        if int(year)%4!=0 and month=='02' and int(day)>28:
            return datetime(int(year),int(month),int(day)).date()

        if int(year)%4==0 and month=='02' and int(day)>29:
            return datetime(int(year),int(month),int(day)).date()

        if int(month)>12 or month=='00':
            return datetime(int(year),int(month),int(day)).date()
        
        if month in ['04','06','09','11'] and int(day)>30:
            return datetime(int(year),int(month),int(day)).date()

        if month in ['01','03','05','07','08','10','12'] and int(day)>31:
            return datetime(int(year),int(month),int(day)).date()


    @property
    def id(self):
        return self.__id    
    @id.setter
    def id(self,value):        
        self.__id=value
    
    @property
    def mr_num(self):
        return self.__mr_num    
    @mr_num.setter
    def mr_num(self,value):
        if len(value)==0 or re.match(r'^\d{5,6}$',value):
            self.__mr_num=value
        else:
            raise ValueError('病案号错误.\n')
    
    @property
    def hospital_num(self):
        return self.__hospital_num    
    @hospital_num.setter
    def hospital_num(self,value):
        if re.match(r'^\d{6,7}$',value) is None:
            raise ValueError('住院号错误.\n')
        self.__hospital_num=value

    @property
    def pat_name(self):
        return self.__pat_name    
    @pat_name.setter
    def pat_name(self,value):
        if len(value)<2:
            raise ValueError('姓名错误.\n')
        self.__pat_name=value

    @property
    def id_num(self):
        return self.__id_num    
    @id_num.setter
    def id_num(self,value):
        if len(value) not in(0,15,18):
            raise ValueError('身份证号错误.\n')
        self.__id_num=value

    @property
    def pd(self):
        return self.__pd    
    @pd.setter
    def pd(self,value):
        if len(value)<2:
            raise ValueError('主要诊断错误.\n')
        self.__pd=value

    @property
    def in_date(self):
        return self.__in_date    
    @in_date.setter
    def in_date(self,value):
        if re.match(self.regex_time,value) is None:
            raise ValueError('入院日期错误，日期格式：yyyy-mm-dd')    

        errormsg=self.validdate(value[:4],value[5:7],value[8:10],'住院日期无效！')
        if not errormsg is None:
            raise ValueError(errormsg)

        self.__in_date=value

    @property
    def dis_date(self):
        return self.__dis_date    
    @dis_date.setter
    def dis_date(self,value):
        if re.match(self.regex_time,value) is None:
            raise ValueError('出院日期错误，日期格式：yyyy-mm-dd')

        errormsg=self.validdate(value[:4],value[5:7],value[8:10],'出院日期无效！')
        if not errormsg is None:
            raise ValueError(errormsg)

        self.__dis_date=value

    @property
    def dis_group(self):
        return self.__dis_group
    @dis_group.setter
    def dis_group(self,value):
        if len(value)<2:
            raise ValueError('出院科室错误\n')
        self.__dis_group=value


    @property
    def doctor(self):
        return self.__doctor    
    @doctor.setter
    def doctor(self,value):
        if len(value)<2:
            raise ValueError('住院医生错误\n')
        self.__doctor=value

    @property
    def dis_way(self):
        return self.__dis_way    
    @dis_way.setter
    def dis_way(self,value):
        if len(value)<2:
            raise ValueError('出院方式错误\n')
        self.__dis_way=value

    @property
    def addr(self):
        return self.__addr    
    @addr.setter
    def addr(self,value):
        self.__addr=value

    @property
    def filing_time(self):
        return self.__filing_time    
    @filing_time.setter
    def filing_time(self,value):
        if re.match(self.regex_time,value) is None:
            raise ValueError('归档日期错误，日期格式：yyyy-mm-dd HH:MM:SS')

        errormsg=self.validdate(value[:4],value[5:7],value[8:10],'归档时间无效！')
        if not errormsg is None:
            raise ValueError(errormsg)

        self.__filing_time=value

    @property
    def is_on(self):        
        return self.__is_on    
    @is_on.setter
    def is_on(self,value=1):
        self.__is_on=value

    def values(self):
        vas=list()
        for value in vars(self).items():
            vas.append(value[1])
        vas=tuple(vas)
        return vas




""" 对应mrborrow表的行 """
class MRBorrow(object):
    def __init__(self,*,id=0,mr_id=0,borrower='',borrow_time='',back_time=''):
        self.__id=id
        self.__mr_id=id
        self.__borrower=borrower
        self.__borrow_time=borrow_time
        self.__back_time=back_time
    
    @property
    def id(self):
        return self.__id    
    @id.setter
    def id(self,value):
        self.__id=value
    
    @property
    def mr_id(self):
        return self.__mr_id    
    @mr_id.setter
    def mr_id(self,value):
        self.__mr_id=value
    
    @property
    def borrower(self):
        return self.__borrower    
    @borrower.setter
    def borrower(self,value):
        self.__borrower=value

    @property
    def borrow_time(self):
        return self.__borrow_time    
    @borrow_time.setter
    def borrow_time(self,value):
        self.__borrow_time=value

    @property
    def back_time(self):
        return self.__back_time    
    @back_time.setter
    def back_time(self,value):
        self.__back_time=value

    
    """ 返回tuple对象，包含全部属性值  """
    def values(self):
        vas=list()
        for value in vars(self).items():
            vas.append(value[1])
        vas=tuple(vas)
        return vas
    


""" 对应mrcopyrecord表的行 """
class MRCopyRecord(object):
    def __init__(self,*,id=0,mr_id=0,copyer_name='',copyer_id_num='',relation='',purpose='',copy_time=''):
        self.__id=id
        self.__mr_id=id
        self.__copyer_name=copyer_name
        self.__copyer_id_num=copyer_id_num
        self.__relation=relation
        self.__purpose=purpose
        self.__copy_time=copy_time

    
    @property
    def id(self):
        return self.__id    
    @id.setter
    def id(self,value):
        self.__id=value
    
    @property
    def mr_id(self):
        return self.__mr_id    
    @mr_id.setter
    def mr_id(self,value):
        self.__mr_id=value
    
    @property
    def copyer_name(self):
        return self.__copyer_name    
    @copyer_name.setter
    def copyer_name(self,value):
        self.__copyer_name=value

    @property
    def copyer_id_num(self):
        return self.__copyer_id_num    
    @copyer_id_num.setter
    def copyer_id_num(self,value):
        self.__copyer_id_num=value

    @property
    def relation(self):
        return self.__relation    
    @relation.setter
    def relation(self,value):
        self.__relation=value

    @property
    def purpose(self):
        return self.__purpose    
    @purpose.setter
    def purpose(self,value):
        self.__purpose=value

    @property
    def copy_time(self):
        return self.__copy_time    
    @copy_time.setter
    def copy_time(self,value):
        self.__copy_time=value

    
    """ 返回tuple对象，包含全部属性值  """
    def values(self):
        vas=list()
        for value in vars(self).items():
            vas.append(value[1])
        vas=tuple(vas)
        return vas



""" 对应 archivesborrow 表的行 """
class ArchivesBorrow(object):
    def __init__(self,*,id=0,arc_id='',borrow_object='',borrower='',borrow_time='',back_time=''):
        self.__id=id
        self.__arc_id=arc_id
        self.__borrow_object=borrow_object
        self.__borrower=borrower
        self.__borrow_time=borrow_time
        self.__back_time=back_time

    
    @property
    def id(self):
        return self.__id    
    @id.setter
    def id(self,value):
        self.__id=value
    
    @property
    def arc_id(self):
        return self.__arc_id    
    @arc_id.setter
    def arc_id(self,value):
        self.__arc_id=value
    
    @property
    def borrow_object(self):
        return self.__borrow_object    
    @borrow_object.setter
    def borrow_object(self,value):
        self.__borrow_object=value

    @property
    def borrower(self):
        return self.__borrower    
    @borrower.setter
    def borrower(self,value):
        self.__borrower=value

    @property
    def borrow_time(self):
        return self.__borrow_time    
    @borrow_time.setter
    def borrow_time(self,value):
        self.__borrow_time=value

    @property
    def back_time(self):
        return self.__back_time    
    @back_time.setter
    def back_time(self,value):
        self.__back_time=value

    
    """ 返回tuple对象，包含全部属性值  """
    def values(self):
        vas=list()
        for value in vars(self).items():
            vas.append(value[1])
        vas=tuple(vas)
        return vas


""" 测试上面的类 """
if __name__ == '__main__':

    print(MRBorrow(id=1,mr_id=1,borrower='lisi',borrow_time='2020-01-01 12:01:00').values())
    print(MRCopyRecord(id=1,mr_id=12345,copyer_name='wangwujie',copy_time='2020-03-11 11:01:01').values())
    print(ArchivesBorrow(id=1,arc_id='20190102-1',borrow_object='财务凭证呢个',borrower='mazi',borrow_time='2020-04-11 11:01:01').values())